//
//  WebrtcViewController.m
//  haolaDoc
//
//  Created by WayneHao on 2017/5/18.
//  Copyright © 2017年 deyi. All rights reserved.
//

#import "WebrtcViewController.h"
#define SERVER_HOST_URL @"https://appr.tc"
@interface WebrtcViewController ()

@end

@implementation WebrtcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isVideoMute = NO;
    _webrtcScrollview.contentSize = CGSizeMake(0,1000);
    
    [self.remoteView setDelegate:self];
    [self.localView setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    //Connect to the room
    [self disconnect];
    self.client = [[ARDAppClient alloc] initWithDelegate:self];
    [self.client setServerHostUrl:SERVER_HOST_URL];
    NSLog(@"这是电话号%@",_roomName);
    [self.client connectToRoomWithId:self.roomName options:nil];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    [self disconnect];
}

- (void)applicationWillResignActive:(UIApplication*)application {
    [self disconnect];
}

- (void)setRoomName:(NSString *)roomName {
    _roomName = roomName;
    self.roomUrl = [NSString stringWithFormat:@"%@/r/%@", SERVER_HOST_URL, roomName];
}
- (void)appClient:(ARDAppClient *)client didChangeState:(ARDAppClientState)state {
    switch (state) {
        case kARDAppClientStateConnected:
            NSLog(@"Client connected.");
            break;
        case kARDAppClientStateConnecting:
            NSLog(@"Client connecting.");
            break;
        case kARDAppClientStateDisconnected:
            NSLog(@"Client disconnected.");
            [self remoteDisconnected];
            break;
    }
}
-(void)remoteDisconnected{
    //如果远程视频轨迹不为空，删除远程视频轨迹的渲染器
    if (self.remoteVideoTrack) [self.remoteVideoTrack removeRenderer:self.remoteView];
    //设置远程视频轨迹为空
    self.remoteVideoTrack = nil;
    //设置远程视图的渲染框架为空
    [self.remoteView renderFrame:nil];
    
}
//断开连接方法
- (void)disconnect {
    //如果client不为空
    if (self.client) {
        //如果本地视频轨迹不为空，删除本地视频轨迹的渲染器
        if (self.localVideoTrack) [self.localVideoTrack removeRenderer:self.localView];
        //如果远程视频轨迹不为空，删除远程视频轨迹的渲染器
        if (self.remoteVideoTrack) [self.remoteVideoTrack removeRenderer:self.remoteView];
        //设置本地视频轨迹为空
        self.localVideoTrack = nil;
        //设置本地视图的渲染框架为空
        [self.localView renderFrame:nil];
        //设置远程视频轨迹为空
        self.remoteVideoTrack = nil;
        //设置远程视图的渲染框架为空
        [self.remoteView renderFrame:nil];
        //重复执行本方法
        [self.client disconnect];
    }
}

//接收本地视频轨迹
- (void)appClient:(ARDAppClient *)client didReceiveLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
    //如果本地视频轨迹不为空
    if (self.localVideoTrack) {
        //删除本地视频轨迹的渲染器
        [self.localVideoTrack removeRenderer:self.localView];
        //设置本地视频轨迹为空
        self.localVideoTrack = nil;
        //设置本地视图的渲染框架为空
        [self.localView renderFrame:nil];
    }
    //设置本地视频轨迹
    self.localVideoTrack = localVideoTrack;
    //添加渲染器
    [self.localVideoTrack addRenderer:self.localView];
}
//接收远程视频轨迹
- (void)appClient:(ARDAppClient *)client didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
    //if (self.remoteVideoTrack) {
        //[self.remoteVideoTrack removeRenderer:self.remoteView];
       // self.remoteVideoTrack = nil;
       // [self.remoteView renderFrame:nil];
    //}
    self.remoteVideoTrack = remoteVideoTrack;
    [self.remoteVideoTrack addRenderer:self.remoteView];
    
}
- (void)appClient:(ARDAppClient *)client didError:(NSError *)error {
    /* Handle the error */
    //新建alertview显示错误信息，取消按钮为“OK”
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@", error]delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    //显示alertview
    [alertView show];
    //执行远程断开连接方法
    [self disconnect];
}
- (void)videoView:(RTCEAGLVideoView *)videoView didChangeVideoSize:(CGSize)size {
    self.localView.frame=CGRectMake(224,53,self.view.frame.size.width/2.5f,self.view.frame.size.height/5.0f);
    self.remoteView.frame=CGRectMake(0, -64,self.view.frame.size.width,self.view.frame.size.height/2.6f);
    [self.view layoutIfNeeded];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)hungupBtn:(id)sender {
}
- (IBAction)videoBtn:(id)sender {
    }


@end
